package boidAlgorithms.implement;

import android.graphics.PointF;
import boidAlgorithms.interfaces.IBoid;
import boidAlgorithms.math.Vector2D;

public class Boid implements IBoid {

	private float mass = 1f;

	private Vector2D position;

	private Vector2D target = new Vector2D(0, 0);
	private Vector2D velocity = new Vector2D(0, 0);

//	private Vector2D steeringForce = new Vector2D(0, 0);
	private Vector2D acceleration = new Vector2D(0, 0);

	private float maxSpeed = 1;
	private float maxForce = 1;
	private float slowingDistance = 200;

	@Override
	public void manualsetPosition(PointF p) {
//		applyforce();
		velocity.length();
	}

	public Boid(Vector2D position) {
		this.position = position;
	}

	public void applyforce(Vector2D force) {
		// TODO Auto-generated method stub
//		force.truncate(maxForce);
//		acceleration = force.mul(1 / mass);
		acceleration=force;
		velocity.addWith(acceleration);
		velocity.truncate(maxSpeed);
		position.addWith(velocity);
	}

	public float getX() {
		return position.x;
	}

	public float getY() {
		return position.y;
	}

	public float getvX() {
		return velocity.x;
	}

	public float getvY() {
		return velocity.y;
	}

	public void aimAt(PointF target) {
		this.target = new Vector2D(target);
	}

	public float getAngle() {
		return (float) Math.atan2(getY(), getX());
	}

//	public void arrival() {
//		Vector2D targetOffset = Vector2D.getSubtraction(target, position);
//
//		float distance = targetOffset.length();
//
//		float rampedSpeed = maxSpeed * (distance / slowingDistance);
//
//		if (rampedSpeed > maxSpeed)
//			rampedSpeed = maxSpeed;
//
////		Vector2D desiredVelocity = targetOffset.mul(rampedSpeed / distance);
//
//		desiredVelocity = Vector2D.getSubtraction(desiredVelocity, velocity);
//		
//		Vector2D steeringForce = desiredVelocity.getSubtractionWith(velocity);
//		applyforce(steeringForce);
//		
//	}

	public void aimAt() {
		seek();
//		arrival();
	}
	
	public void seek(){
		Vector2D desiredVelocity = Vector2D.getSubtraction(target, position);
		desiredVelocity.setSizeTo(maxSpeed);
		Vector2D steeringForce = Vector2D.getSubtraction(desiredVelocity, velocity);
		steeringForce.setSizeTo(maxForce);
		applyforce(steeringForce);
	}

	public float getSpeed() {
		return this.velocity.getModule();
	}
}
